<template>
  <el-dialog :title="dataForm.id ? '评价模板新增' : '评价模板修改'" :close-on-click-modal="false" :visible.sync="visible"
    fullscreen center append-to-body>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form size="medium" label-width="160px">
      <el-form-item label="评价模板类型">
        <el-select v-model="dataForm.typeWord" placeholder="请选择">
          <el-option v-for="item in questionTypeList" :value="item.value" :label="item.label" :key="item.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="前言">
        <el-input type="textarea" v-model="dataForm.preface"></el-input>
      </el-form-item>

      <el-table :data="dataForm.problemList" border style="margin: 15px 0;">
        <el-table-column prop="problemName" label="问题"></el-table-column>
        <el-table-column prop="isShowWechat" label="展示状态">
          <template slot-scope="scope">
            <el-radio-group v-model="scope.row.isShowWechat">
              <el-radio :label="1">会员在手机端评价时，可以看到此问题</el-radio>
              <el-radio :label="0">会员在手机端评价时，不会看到此问题</el-radio>
            </el-radio-group>
          </template>
        </el-table-column>
        <el-table-column width="100">
          <template slot="header" slot-scope="scope">
            <el-button type="primary" @click="openQuestionDialog">添加问题</el-button>
          </template>
          <template slot-scope="scope">
            <el-button type="danger" @click="deleteHandle(scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-form-item label="调查问卷结束语">
        <el-input type="textarea" v-model="dataForm.concludingRemarks"></el-input>
      </el-form-item>
      <el-form-item label="奖励方式">
        <el-radio-group v-model="dataForm.rewardWay">
          <el-radio :label="0">无奖励</el-radio>
          <el-radio :label="1">奖励</el-radio>
        </el-radio-group>
        <el-input v-if="dataForm.rewardWay == 1" v-model="dataForm.integral" style="width: 160px;margin-left: 25px;">
          <template slot="append">积分</template>
        </el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">停用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button size="medium" type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>保 存</el-button>
    </span>

    <question-list v-if="listVisible" ref="listDialog" @refreshData="getQuestionData"></question-list>
  </el-dialog>
</template>

<script>
  import { remote } from '@/api/admin/dict'
  import { getQuestionTypeList } from '@/api/mp/wxevaluatetype'
  import { getWxEvaluationTemplateObj, addWxEvaluationTemplateObj, putWxEvaluationTemplateObj } from '@/api/mp/wxevaluationtemplate'

  import QuestionList from './QuestionList'
  export default {
    data() {
      return {
        visible: false,
        listVisible: false,
        typeVisible: false,
        ischeck: false,
        questionTypeList: [],
        dataForm: {
          id: '',
          typeWord: '',
          preface: '',
          problemList: [],
          concludingRemarks: '',
          rewardWay: 0,
          integral: '',
          status: 1
        }
      }
    },
    components:{
      QuestionList
    },
    methods: {
      init(id) {
        this.visible = true
        this.dataForm = {
          typeWord: '',
          preface: '',
          problemList: [],
          concludingRemarks: '',
          rewardWay: 0,
          integral: '',
          status: 1
        }
        this.dataForm.id = id || 0
        // this.getQuestionType()
        remote('evaluate_type').then(result => {
          this.questionTypeList = result.data.data
        })
        if(this.dataForm.id){
          this.getData()
        }
      },
      getQuestionType(){
        getQuestionTypeList().then(response => {
          this.questionTypeList = response.data.data
        })
      },
      getData(){
        getWxEvaluationTemplateObj(this.dataForm.id).then(response => {
          this.dataForm = response.data.data
        })
      },
      // 保存
      dataFormSubmit() {
        if(this.dataForm.id){
          putWxEvaluationTemplateObj(this.dataForm).then(response => {
            this.$message.success('修改成功')
            this.visible = false
            this.$emit('refreshDataList')
          })
        }else {
          addWxEvaluationTemplateObj(this.dataForm).then(response => {
            this.$message.success('修改成功')
            this.visible = false
            this.$emit('refreshDataList')
          })
        }
      },
      // 打开问题列表
      openQuestionDialog(){
        this.listVisible = true

        let list = []

        this.dataForm.problemList.forEach(item => {
          list.push(item.id)
        })
        this.$nextTick(() => {
          this.$refs.listDialog.init(JSON.stringify(list))
        })
      },
      // 添加问题
      getQuestionData(data){
        data.isShowWechat = 1
        this.dataForm.problemList.push(data)
      },
      // 删除问题
      deleteHandle(index){
        this.dataForm.problemList.splice(index,1)
      }
    }
  }
</script>

<style>
  table {
    width: 100%;
  }

  tr td {
    border: solid 1px #e7e7e7;
  }
</style>
